# coding:utf-8
"""
数据分析工具 数据清洗  数据处理 工具：
	pandas：去重  填充空白   修改数据   数据重排   随机采样   在html文件中读取表格
	numpy：矩阵运算处理
	matplotlib：画图，描绘可视化数据
	pageNum=1'
"""
import requests
from bs4 import BeautifulSoup
import pandas
from urllib.parse import urlencode
_url = 'http://s.askci.com/stock/a/?'


def get_page(numb):
	try:
		# 模拟浏览器行为
		headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36'}
		t_url = {
			'pageNum': numb,
			'reportTime': '2019-04-09'
		}
		new_url = _url + urlencode(t_url)
		response = requests.get(new_url, headers=headers)
		if response.status_code == 200:
			return response.text
		return None
	except Exception as e:
		print(e)


def parse_page(_html):
	try:
		soup = BeautifulSoup(_html, 'html.parser')
		content = soup.select('#myTable04')[0]
		table = pandas.read_html(content.prettify(), header=0)[0]
		return table
	except Exception as e:
		print(e)


if __name__ == "__main__":
	for i in range(1, 3):
		_html = get_page(i)
		tb = parse_page(_html)
		tb.to_csv('股票.csv', mode='a', encoding='utf_8_sig', header='1', index=0)
